package com.zyh.scs.common.core.util;


import com.zyh.scs.common.core.exception.OpenException;
import com.zyh.scs.common.core.exception.OpenResponseCode;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 对象复制
 *
 * @author hezhao
 */
public class BeanCopyUtil {
	private BeanCopyUtil(){}
	public static <T> T copy(final Object source, final Class<T> clazz) {
		if (source == null) {
			return null;
		}
		try {
			T t = clazz.newInstance();
			BeanUtils.copyProperties(source, t);
			return t;
		} catch (Exception e) {
			throw new OpenException(OpenResponseCode.COPY_ERROR);
		}
	}

	public static <E, V> List<V> copyListToList(List<E> source, Class<V> clazz) {
		if (source == null){
			return new ArrayList<>();
		}
		return source.stream().map(pojo -> copy(pojo, clazz)).collect(toList());
	}

}
																														
